/**
 * 
 */
package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipCheckDetail;
import cc.comac.mom.bean.EquipCheckDetailBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipCheckDetailDao;
import cc.comac.mom.entity.EquipCheckDetailEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

/**
* @Description: 
* @author: fyt 
* @date: 2020年7月21日下午2:12:47
*/
@Component
@Transactional
public class EquipCheckDetailDataHandler extends ServiceImpl<EquipCheckDetailDao, EquipCheckDetailEntity>{

	@Autowired
	private EquipCheckDetailDao equipCheckDetailDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean saveBatch(List<EquipCheckDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipCheckDetailEntity> listEquipCheckDetailEntity = new ArrayList<EquipCheckDetailEntity>();
		for(EquipCheckDetail item:list) {
			EquipCheckDetailEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			if(innerData.getCheckId() <1) {
				throw new RuntimeException("盘点信息不存在");
			}
			if(innerData.getDevId() <1) {
				throw new RuntimeException("设备信息不存在");
			}
			innerData.setCheckAbnormal(1);
			innerData.setCheckStatus(MomConstants.EQUIP_CHECK_STATUS_NOCHECKED);
			baseEntityDataUtil.setBaseEntityData(innerData);
			
			listEquipCheckDetailEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEquipCheckDetailEntity);
		if(!saveBatch) {
			throw new RuntimeException("保存失败");
			
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipCheckDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		
		for(EquipCheckDetail item:list) {
			EquipCheckDetailEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			listIds.add(innerData.getId());
		}
		List<EquipCheckDetailEntity> selectBatchIdsList = equipCheckDetailDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipCheckDetailEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		
		if(!updateBatchById) {
			throw new RuntimeException("删除失败");
		}

		return resultBean;
	}
	/**
	 * 
	* @Title: equipCheckUpdate 
	* @Description: 设备盘点确认
	* @param list
	* @return
	*
	 */	
	public ResultBean equipCheckDetailChecked(List<EquipCheckDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listId = new ArrayList<Long>();
		for(EquipCheckDetail item :list) {
			if(item.getInnerData() == null) {
				throw new RuntimeException("数据为空");
			}
			listId.add(item.getInnerData().getId());			
		}
		List<EquipCheckDetailEntity> selectBatchIds = equipCheckDetailDao.selectBatchIds(listId);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipCheckDetailEntity item :selectBatchIds) {
			item.setCheckStatus(MomConstants.EQUIP_CHECK_STATUS_CHECKED);
			baseEntityDataUtil.updateBaseEntityData(item);
		}
		boolean updateBatchById = super.updateBatchById(selectBatchIds);
		if(!updateBatchById) {
			throw new RuntimeException("设备盘点失败！");
		}
		return resultBean;
	}

	public List<EquipCheckDetail> getByCheckIds(List<Long> listCheckIds){
		if(listCheckIds != null && listCheckIds.size()>0) {
			QueryWrapper<EquipCheckDetailEntity> queryWrapper = new QueryWrapper<EquipCheckDetailEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.in("check_id", listCheckIds);
			List<EquipCheckDetailEntity> selectList = equipCheckDetailDao.selectList(queryWrapper);
			if(selectList != null && selectList.size()>0) {
				ArrayList<EquipCheckDetail> listEquipCheckDetail = new ArrayList<EquipCheckDetail>();
				for(EquipCheckDetailEntity item:selectList) {
					EquipCheckDetail equipCheckDetail = new EquipCheckDetail();
					equipCheckDetail.setInnerData(item);
					listEquipCheckDetail.add(equipCheckDetail);
				}
				return listEquipCheckDetail;
			}
			return null;
		}
		return null;
		
	}
	
	public ResultDev selectEquipCheckDetailByCheckId (EquipCheckDetailBean obj) {
		ResultDev resultDev = new ResultDev();
		if(obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipCheckDetailBean> listEquipCheckDetailBean = equipCheckDetailDao.selectEquipCheckDetailByCheckId(obj);
		long selectCountByCheckId = equipCheckDetailDao.selectCountByCheckId(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)selectCountByCheckId / obj.getPageSize());
		resultDev.setEntityList(listEquipCheckDetailBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)selectCountByCheckId);
		return resultDev;
	}
}
